package com.nginx.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nginx.web.entity.Config;
import com.nginx.web.mapper.ConfigMapper;
import com.nginx.web.service.ConfigService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lyc
 * @since 2025-01-13
 */
@Service
public class ConfigServiceImpl extends ServiceImpl<ConfigMapper, Config> implements ConfigService {

    public void saveConfig(Map<String,String> map) {
        List<String> list = Arrays.asList("boolDel", "createTime", "updateTime");
        for (String key : list) {
            map.remove(key);
        }
        for(Map.Entry<String,String> entry : map.entrySet()){
            LambdaQueryWrapper<Config> wrapper = Wrappers.lambdaQuery(Config.class)
                    .eq(Config::getKey, entry.getKey());
            int count = this.count(wrapper);
            if(count == 0){
                Config config = new Config(entry.getKey() , "/" );
                this.save(config);
            }
            Config config = getOne(wrapper);
            config.setValue(entry.getValue());
            updateById(config);
        }
    }

    public Map<String, String> getConfigList() {
        List<Config> list = this.list();
        Map<String, String> map = new HashMap<>();
        for (Config config : list) {
            map.put(config.getKey() , config.getValue());
        }
        return map;
    }
}
